#include<iostream>
#include<unistd.h>
#include<pthread.h>
using namespace std;

//有1000张票待抢
int ticket = 1000;

void* routine(void* msg)
{
    while(ticket>0)  //剩余票数大于0就一直抢票
    {
        usleep(1000);
        ticket--;
        printf("%s get ticket : %d\n",(char*)msg,ticket);
    }
    return nullptr;
}

int main()
{
    pthread_t tid[4];  //创建四个新线程一起抢票
    //创建新线程
    for(int i=0;i<4;++i)
    {
        char* buff = new char[32];
        snprintf(buff,sizeof(buff),"thread%d",i);
        pthread_create(tid+i,nullptr,routine, buff);
    }

    //线程等待
    for(int i=0;i<4;++i)
    {
        pthread_join(tid[i],nullptr);
    }

    return 0;
}